package com.psmz.mqim.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.xml.stream.XMLStreamException;

import com.ibm.mq.MQException;
import com.psmz.mqim.job.MessagePoller;
import com.psmz.mqim.model.IMMessage;
import com.psmz.mqim.model.IMMessageType;
import com.psmz.mqim.service.InstantMessaging;
import com.psmz.mqim.settings.MQSeriesSettings;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7038394132371475709L;
	private JPanel contentPane;
	private JTextField contentField;
	private static MQSeriesSettings settings;
	private static InstantMessaging messaging;
	private static MessagePoller poller;
	private JTextField sendToField;
	private JLabel sendToLabel;
	private JLabel contentLabel;
	private static String userName = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		settings = new MQSeriesSettings();
		final InitDialog dialog = new InitDialog(settings);
		dialog.addComponentListener(new java.awt.event.ComponentListener() {

			@Override
			public void componentShown(ComponentEvent e) {

			}

			@Override
			public void componentResized(ComponentEvent e) {

			}

			@Override
			public void componentMoved(ComponentEvent e) {
			}

			@Override
			public void componentHidden(ComponentEvent e) {
				messaging = new InstantMessaging(settings);
				poller = new MessagePoller(messaging);
				userName = dialog.getUserName();
				initUi();
				initPollerJob();
			}

		});
		dialog.setVisible(true);

	}

	private static void initPollerJob() {
		Thread thread = new Thread(poller);
		thread.start();
	}

	private static void initUi() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					System.err.println(e.toString());
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 653, 474);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JTextArea messagesArea = new JTextArea();
		messagesArea.setBounds(5, 5, 607, 321);
		contentPane.add(messagesArea);
		TextAreaUpdater updater = new TextAreaUpdater(messagesArea);
		poller.addObserver(updater);

		contentField = new JTextField();
		contentField.setBounds(81, 370, 324, 20);
		contentPane.add(contentField);
		contentField.setColumns(10);

		sendToField = new JTextField();
		sendToField.setBounds(516, 370, 86, 20);
		contentPane.add(sendToField);
		sendToField.setColumns(10);

		sendToLabel = new JLabel("Do");
		sendToLabel.setBounds(464, 373, 46, 14);
		contentPane.add(sendToLabel);

		contentLabel = new JLabel("Wiadomosc");
		contentLabel.setBounds(10, 373, 61, 14);
		contentPane.add(contentLabel);

		JButton btnNewButton = new JButton("Wyslij");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IMMessage message = new IMMessage();
				message.setFromUser(userName);
				message.setSendDate(new Date());
				message.setToUser(sendToField.getText());
				message.setPayload(contentField.getText());
				message.setType(IMMessageType.TEXT_MESSAGE);
				try {
					messaging.sendInstantMessage(message);
				} catch (MQException e1) {
					JOptionPane.showMessageDialog(MainFrame.this,
							"Blad MQSERIES, powod: " + e1.toString(), "Blad!",
							JOptionPane.ERROR_MESSAGE);
					System.err.println(e1.toString());
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(MainFrame.this,
							"Blad wejscia/wyjscia! Powod: " + e1.toString(),
							"Blad!", JOptionPane.ERROR_MESSAGE);
					System.err.println(e1.toString());
				} catch (XMLStreamException e1) {
					JOptionPane.showMessageDialog(MainFrame.this,
							"Blad serializacji xml! Powod: " + e1.toString(),
							"Blad!", JOptionPane.ERROR_MESSAGE);
					System.err.println(e1.toString());
				}
			}
		});
		btnNewButton.setBounds(81, 401, 89, 23);
		contentPane.add(btnNewButton);
	}
}
